package com.hotel.api.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hotel.api.entity.UserRepairOrder;
import com.hotel.api.service.RepairService;

import com.hotel.api.utils.JSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * 保修单操作Controller
 *
 * @Author 涛涛
 * @Create 2019/8/29
 */
@Api("报修管理")
@RestController
@RequestMapping("/repair")
public class RepairController {


    @Autowired
    RepairService repairService;


    /**
     * 工作人员接受订单,将当前订单的id传入
     *
     * @param
     * @param
     * @return
     */

    @ApiOperation("订单状态更改/工作人员接单/用户确认完成订单/用户评价")
    @PostMapping("/updateRepairOrderState")
    public JSONResult workAcceptOrder(UserRepairOrder userRepairOrder) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = simpleDateFormat.format(new Date());
        if (userRepairOrder.getState() == 1 || userRepairOrder.getState() == 2 || userRepairOrder.getState() == 3) {
            userRepairOrder.setUpdateTime(date);
            repairService.update(userRepairOrder);
            return JSONResult.build(200, "更改状态成功", null);
        } else if (userRepairOrder.getState() == 4) {
            if (userRepairOrder.getAssess() == null) {
                return JSONResult.errorMsg("请填写评价");
            }
            userRepairOrder.setFinishTime(date);
            repairService.update(userRepairOrder);
            return JSONResult.build(200, "评价成功", null);
        }
        return JSONResult.errorMsg("操作失败");

    }

    /**
     * 查询所有保修单的信息集合
     *
     * @return
     */
    @ApiOperation("获取所有的报修单信息")
    @PostMapping("/selectAllRepairOrders")
    public JSONResult selectAll(@RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
                                @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit) {
        PageHelper.startPage(page, limit);
        List<UserRepairOrder> list = repairService.selectAll();
        PageInfo<UserRepairOrder> pageInfo = new PageInfo<>(list);
        if (pageInfo != null) {
            return JSONResult.build(200, "查看成功", pageInfo);
        }
        return JSONResult.errorMsg("暂无数据");
    }


    /**
     * 用户填写保修单
     *
     * @param userRepairOrder 保修单信息
     * @return
     */
    @ApiOperation("用户填写报修单信息")
    @PostMapping("/insertOrder")
    public JSONResult insertOrder(UserRepairOrder userRepairOrder) {
        if (userRepairOrder.toString().isEmpty()) {
            return JSONResult.errorMsg("数据为空");
        } else {
            //设置创建时间
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date = df.format(new Date());
            userRepairOrder.setCreateTime(date);
            userRepairOrder.setDelState((byte) 0);
//            order.setUpdateTime(date);
            System.out.println("发布订单成功");
            repairService.insert(userRepairOrder);
            return JSONResult.build(200, "提交成功", null);
        }
    }

    /**
     * 查看用户的所有保修订单信息集合
     *
     * @param uid 用户id
     * @return
     */
    @ApiOperation("用户查看自己的报修单信息")
    @PostMapping("/userRepairOrders")
    public JSONResult findAllUserOrder(Integer uid) {
        return JSONResult.build(200, "查询成功", repairService.selectRepairOrderByUid(uid));
    }


    /**
     * 查看指定保修单信息
     *
     * @param id 保修单id
     * @return
     */
    @ApiOperation("获取指定报修单信息")
    @PostMapping("/findOrder")
    public JSONResult findOrder(Integer id) {
        return JSONResult.build(200, "查询成功", repairService.selectRepairOrderById(id));
    }

    /**
     * 根据报单状态查看用户保修单信息
     *
     * @param uid   保修单id
     * @param state 报单状态
     * @return
     */
    @ApiOperation("根据报修单状态查看用户报修单信息")
    @PostMapping("/findRepaireOrderByState")
    public JSONResult findOrderByState(Integer uid, String state) {
        return JSONResult.build(200, "查询成功", repairService.selectRepairOrderByUidAndState(uid, Byte.valueOf(state)));
    }


    /**
     * 用户取消报修单
     *
     * @param order 保修单id  updateRepairOrderAssess(UserRepairOrder userRepairOrder);
     * @return
     */
    @ApiOperation("用户取消报修单")
    @PostMapping("/deleteOrder")
    public JSONResult deleteOrder(UserRepairOrder order) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = df.format(new Date());
        order.setFinishTime(date);
        //订单取消，订单状态更改
        order.setDelState((byte) 1);
        return JSONResult.build(200, "成功取消", repairService.deleteRepairOrderById(order));
    }
}
